"""告警类型和严重程度定义"""

from enum import Enum
from typing import Dict, Tuple

class AlertType(Enum):
    """告警类型枚举"""
    # 头盔佩戴相关
    PEDESTRIAN_VIOLATION = "pedestrian_violation"
    VEHICLE_OVERLAP = "vehicle_overlap"
    
    # 区域违规相关
    ZONE_VIOLATION = "zone_violation"
    
    # 方向违规相关
    WRONG_DIRECTION = "wrong_direction"
    
    # 停车违规相关
    ILLEGAL_PARKING = "illegal_parking"
    
    # 系统相关
    SYSTEM_ERROR = "system_error"
    CAMERA_DISCONNECTED = "camera_disconnected"
    MODEL_LOAD_FAILURE = "model_load_failure"
    
    # 性能相关
    HIGH_CPU_USAGE = "high_cpu_usage"
    HIGH_MEMORY_USAGE = "high_memory_usage"
    LOW_FPS = "low_fps"

class SeverityLevel(Enum):
    """告警严重程度枚举"""
    INFO = "info"
    WARNING = "warning"
    MEDIUM = "medium"  # 新增中等严重程度
    ERROR = "error"
    CRITICAL = "critical"

# 告警类型与默认严重程度映射
ALERT_TYPE_TO_SEVERITY: Dict[AlertType, SeverityLevel] = {
    AlertType.PEDESTRIAN_VIOLATION: SeverityLevel.WARNING,
    AlertType.VEHICLE_OVERLAP: SeverityLevel.MEDIUM,
    AlertType.ZONE_VIOLATION: SeverityLevel.WARNING,
    AlertType.WRONG_DIRECTION: SeverityLevel.WARNING,
    AlertType.ILLEGAL_PARKING: SeverityLevel.WARNING,
    AlertType.SYSTEM_ERROR: SeverityLevel.ERROR,
    AlertType.CAMERA_DISCONNECTED: SeverityLevel.CRITICAL,
    AlertType.MODEL_LOAD_FAILURE: SeverityLevel.CRITICAL,
    AlertType.HIGH_CPU_USAGE: SeverityLevel.WARNING,
    AlertType.HIGH_MEMORY_USAGE: SeverityLevel.WARNING,
    AlertType.LOW_FPS: SeverityLevel.WARNING
}

# 严重程度颜色映射
SEVERITY_COLORS: Dict[SeverityLevel, Tuple[int, int, int]] = {
    SeverityLevel.INFO: (0, 191, 255),      # 深天蓝色
    SeverityLevel.WARNING: (255, 215, 0),   # 金色
    SeverityLevel.MEDIUM: (255, 140, 0),    # 橙色
    SeverityLevel.ERROR: (255, 69, 0),      # 橙红色
    SeverityLevel.CRITICAL: (220, 20, 60)   # 深红色
}

# 严重程度描述映射
SEVERITY_DESCRIPTIONS: Dict[SeverityLevel, str] = {
    SeverityLevel.INFO: "信息",
    SeverityLevel.WARNING: "警告",
    SeverityLevel.MEDIUM: "中等",
    SeverityLevel.ERROR: "错误",
    SeverityLevel.CRITICAL: "严重"
}

# 告警类型描述映射
ALERT_TYPE_DESCRIPTIONS: Dict[AlertType, str] = {
    AlertType.PEDESTRIAN_VIOLATION: "行人违规",
    AlertType.VEHICLE_OVERLAP: "车辆重叠",
    AlertType.ZONE_VIOLATION: "区域违规",
    AlertType.WRONG_DIRECTION: "逆向行驶",
    AlertType.ILLEGAL_PARKING: "违章停车",
    AlertType.SYSTEM_ERROR: "系统错误",
    AlertType.CAMERA_DISCONNECTED: "摄像头断开连接",
    AlertType.MODEL_LOAD_FAILURE: "模型加载失败",
    AlertType.HIGH_CPU_USAGE: "CPU使用率过高",
    AlertType.HIGH_MEMORY_USAGE: "内存使用率过高",
    AlertType.LOW_FPS: "帧率过低"
}

# 告警类别分组
ALERT_CATEGORIES = {
    "traffic_violations": [
        AlertType.PEDESTRIAN_VIOLATION,
        AlertType.ZONE_VIOLATION,
        AlertType.WRONG_DIRECTION,
        AlertType.ILLEGAL_PARKING,
        AlertType.VEHICLE_OVERLAP
    ],
    "system_issues": [
        AlertType.SYSTEM_ERROR,
        AlertType.CAMERA_DISCONNECTED,
        AlertType.MODEL_LOAD_FAILURE
    ],
    "performance_issues": [
        AlertType.HIGH_CPU_USAGE,
        AlertType.HIGH_MEMORY_USAGE,
        AlertType.LOW_FPS
    ]
}

# AlertType类的静态方法
def get_alert_name(alert_type_value):
    """获取告警类型名称"""
    for alert_type, description in ALERT_TYPE_DESCRIPTIONS.items():
        if alert_type.value == alert_type_value:
            return description
    return "未知告警类型"

def get_alert_description(alert_type_value):
    """获取告警类型描述"""
    for alert_type, description in ALERT_TYPE_DESCRIPTIONS.items():
        if alert_type.value == alert_type_value:
            return description
    return "未知告警类型"

def get_alert_category(alert_type_value):
    """获取告警类别"""
    for category, types in ALERT_CATEGORIES.items():
        for alert_type in types:
            if alert_type.value == alert_type_value:
                return category
    return "other"

def get_default_severity(alert_type_value):
    """获取告警默认严重程度"""
    for alert_type, severity in ALERT_TYPE_TO_SEVERITY.items():
        if alert_type.value == alert_type_value:
            return severity.value
    return SeverityLevel.WARNING.value

# 为AlertType类添加静态方法
AlertType.get_alert_name = staticmethod(get_alert_name)
AlertType.get_alert_description = staticmethod(get_alert_description)
AlertType.get_alert_category = staticmethod(get_alert_category)
AlertType.get_default_severity = staticmethod(get_default_severity)

# 为SeverityLevel类添加静态方法
SeverityLevel.get_level_name = staticmethod(lambda level_value: SEVERITY_DESCRIPTIONS.get(next((s for s in SeverityLevel if s.value == level_value), None), "未知"))
SeverityLevel.get_level_color = staticmethod(lambda level_value: SEVERITY_COLORS.get(next((s for s in SeverityLevel if s.value == level_value), None), (128, 128, 128)))